<?php
if (!defined('FosfoaSystem')){exit(1);}
import($GLOBALS['spConfig']['controller_path'] . '/general.php');
/**
 * RPPAY支付管理控制器
 * @author Fosf
 * @version 1.0
 * @created 2011-04-11
 */

class rppay extends general
{
    var $orders_status;

    function __construct()
    { // 公用
        parent::__construct(); // 这是必须的
        $this->URL = $_SERVER['REQUEST_URI'];
    }

    
    //RPPAY支付    接收返回数据，并返回给客户看
    function CheckoutRppay()
    {
        //dump($this->spArgs());
        //dump($this->orders_status);
        //exit;
        //$referer=$this->spArgs('referer');
        $siteid=$this->spArgs('siteid');//本站接口方提供的站点ID
        $order_sn=$this->spArgs('order_sn');//本站定单号
        $total=$this->spArgs('total');//交易的总金额
        $verifyCode=$this->spArgs('verifyCode');//交易验证码
        $verified='rp'.urldecode($this->spArgs('verified'));//交易状态
        $currency=$this->spArgs('currency');//交易币种
        $transactionid=$this->spArgs('transactionid');//支付接口方的交易编号
        $payment=$this->spArgs('payment');//支付方式
        
        //根据返回的交易验证，确定是否与本地相配
        if(strtolower($verifyCode)==strtolower(md5($order_sn.RPPAY_SITEID.RPPAY_MIYAO)))
        {
            $_SESSION['orders_id']='';
            $_SESSION['orders_sn']='';
            
            foreach ($this->orders_status as $key=>$val)
            {
                //根据返回的字符确定订单状态的所属ID                
                if($val['orders_status_name']==$verified){
                    $verified=$val['orders_status_id'];                   
                }
            }
        
            //echo $verified2;
            
            $conditions = array("orders_sn" => $order_sn);
            //检查定单是否存在
            $order=spClass('orderModel')->find($conditions);
            if($order)
            {
            //存在就修改订单数据
            $data=array('orders_status'=>$verified,'rppay_id'=>$transactionid,'last_modified'=>time());
            spClass('orderModel')->update_order($data,$order_sn,$order['orders_id']);
            
            //订单的交易过程
            $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'date_added'=>time());
            spClass('orders_status_historyModel')->Create($data2);
            
            //如果为支付成功就删除购物车,并发通知邮件
            $status_approve=array('100','106','110');
            if(in_array($verified,$status_approve)){
                $customers_basket = spClass('customers_basketModel');
                $customers_id = $this->userarr['customers_id']; //此数据来自初始化
                $conditions = array('customers_id' => $customers_id);
                $customers_basket->delete($conditions); // 删除记录
                //这里将放置付款成功提醒邮件
                    $ordermail = spClass('email_templateModel')->order_send($order_sn);//发送订单邮件
                    if ($ordermail)
                    {
                        $sendmailok = true;
                    }
                $this->jump(spUrl('main','CheckoutSuccess'));
            }else{
               $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror'); 
            }
            
            
            }else{
                //echo "update status error";
                //订单不存在
                $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
                exit;
            }
        }else{
            //echo "update status error";
            //交易验证失败
            $this->jump(spUrl('main','CheckoutSuccess').'?code=payerror');
            exit;
        }
    }
    
    //接收返回数据，不显示
    function RppayCheck()
    {
        //dump($this->spArgs());
        //dump($this->orders_status);
        //exit;
        $referer=$this->spArgs('referer');
        $siteid=$this->spArgs('siteid');
        $order_sn=$this->spArgs('_order_sn');
        $total=$this->spArgs('_amount');
        $verifyCode=$this->spArgs('_verifyCode');
        $verified='rp'.$this->spArgs('_verified');
        $currency=$this->spArgs('_currency');
        $transactionid=$this->spArgs('_transactionid');
        if(strtolower($verifyCode)==strtolower(md5($order_sn.RPPAY_SITEID.RPPAY_MIYAO)))
        {
            foreach ($this->orders_status as $key=>$val)
            {
                //echo $val['orders_status_name'];                
                if($val['orders_status_name']==$verified){
                    $verified=$val['orders_status_id'];
                    //echo 'aaa';                    
                }
            }
            $conditions = array("orders_sn" => $order_sn);
            if(spClass('orderModel')->find($conditions)){
            
            $data=array('orders_status'=>$verified,'last_modified'=>time());
            spClass('orderModel')->update($conditions,$data);
            $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'customer_notified' => 0,'comments' => 'Pay notice [transactionid:'.$transactionid.'- order_sn:' . $order_sn .'- Currency:' . $currency . '-total:'.$total.']','date_added'=>time());
            spClass('orders_status_historyModel')->Create($data2);
            //$this->jump('account.html');
            }else{
                
                echo "update status error";
                exit;
            }
        }else{
            echo "update status error";
            exit;
        }
        echo "update complete";  
    }
    
    
    //master卡返回数据，不显示
    public function rp_check()
    {
        import("nusoap.php");
        $transactionid=$this->spArgs('transactionid');
        $verifyCode=$this->spArgs('_verifyCode');
        
        $server = "http://check.realypay.com/ValidateOrder.asmx?WSDL";
        $client = new nusoap_client($server, true);
    
        $param = array('cipherkey' => RPPAY_MIYAO, "transactionid"=>$transactionid);
        $result = $client->call('Query', array('parameters' => $param), '', '', false, true, 'document', 'encoded');
        $re = $result['QueryResult'];
    
        $rearr = explode("&",$re);
        $r_verified = explode("=",$rearr[6]);
        $r_total = explode("=",$rearr[3]);
        $r_currency = explode("=",$rearr[5]);
        $r_ordersn = explode("=",$rearr[2]);
        $r_siteid = explode("=",$rearr[1]);
        
        
        $siteid=$r_verified[1];
        $order_sn=$r_ordersn[1];
        $total=$r_total[1];
        $verified='rp'.$r_verified[1];
        $currency=$r_currency[1];
        
        //dump($re);
        //dump($rearr);
        //exit;
        if($order_sn!="" && $transactionid!=="" && $currency!="" && $total!="")
        {
            foreach ($this->orders_status as $key=>$val)
            {
                //echo $val['orders_status_name'];                
                if($val['orders_status_name']==$verified){
                    $verified=$val['orders_status_id'];
                    //echo 'aaa';                    
                }
            }
        
            //echo $verified2;
            
            $conditions = array("orders_sn" => $order_sn);
            if(spClass('orderModel')->find($conditions)){
            
            $data=array('orders_status'=>$verified,'last_modified'=>time());

            spClass('orderModel')->update($conditions,$data);
            $data2=array('orders_sn'=>$order_sn,'orders_status_id'=>$verified,'customer_notified' => 0,'comments' => 'Pay notice [transactionid:'.$transactionid.'- order_sn:' . $order_sn .'- Currency:' . $currency . '-total:'.$total.']','date_added'=>time());
            spClass('orders_status_historyModel')->Create($data2);
            //$this->jump('account.html');
            }else{
                
                echo "update status error";
                exit;
            }
        }else{
            echo "update status error";
            exit;
        }
        echo "update complete";
        
        
    }
}